package com.woniuxy.usercoachassemble.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.usercoachassemble.commons.ToData;
import com.woniuxy.usercoachassemble.entity.CoachCourse;
import com.woniuxy.usercoachassemble.entity.CoachInfo;
import com.woniuxy.usercoachassemble.entity.Course;
import com.woniuxy.usercoachassemble.entity.YogaGymSignedCoach;
import com.woniuxy.usercoachassemble.service.CoachCourseService;
import com.woniuxy.usercoachassemble.service.CoachInfoService;
import com.woniuxy.usercoachassemble.service.CourseService;
import com.woniuxy.usercoachassemble.service.YogaGymSignedCoachService;
import org.example.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.ref.WeakReference;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2020-07-18
 */
@RestController
@RequestMapping("yoga-gym-signed-coach")
public class YogaGymSignedCoachController {
    @Autowired
    private CoachInfoService coachInfoService;
    @Autowired
    private CoachCourseService coachCourseService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private YogaGymSignedCoachService yogaGymSignedCoachService;
    /**
     * @description:TODO(查询当前场馆教练)
     * @param userid
     * @param status 2.签约中 3.已解约
     * @return:  org.example.Result
     * @author: Fy浪迹、
     * @date: 2020/7/18 19:26
     */
    @GetMapping("selectSignContractCoach")
    public Result selectSignContractCoach(HttpServletRequest request, Integer userid, Integer status, Long current, Long size){
        // 保存的用户id
        Integer hid =  Integer.parseInt(request.getHeader("userId"));

        ToData toData = yogaGymSignedCoachService.selectSignContractCoachByGymId(hid,status,current,size);

        return Result.Success(toData);
    }

    /**
     * @description:TODO(解约教练)
     * @param coachId
     * @return:  org.example.Result
     * @author: Fy浪迹、
     * @date: 2020/7/18 20:03
     */
    @PostMapping("terminateAgreementCoach")
    public Result terminateAgreementCoach(HttpServletRequest request,Integer coachId){
        // 保存的场馆id
        Integer hid = Integer.parseInt(request.getHeader("userId"));

        // 根据场馆id以及教练id查询签约信息
        YogaGymSignedCoach ygsc = yogaGymSignedCoachService.selectByCoachIdAndGymId(hid, coachId);


        if(ygsc == null){
            return Result.Error(600,"并未签约该教练!");
        }

        if(ygsc.getStatus() != 2){
            return Result.Error(600,"该教练已经解约或还未签约!");
        }
        System.out.println("#####" + coachId);
        boolean b =  yogaGymSignedCoachService.updataStatus(coachId,3);

        if(b){
            Result success = Result.Success();
            success.setMessage("解约成功");
            return success;
        }

        return  Result.Error(600,"服务器繁忙!");
    }

    /**
     * @description:TODO(签约教练)
     * @param coachId 教练id
     * @return:  org.example.Result
     * @author: Fy浪迹、
     * @date: 2020/7/18 20:58
     */
    @PostMapping("signingCoach")
    public Result signingCoach(HttpServletRequest request, Integer coachId){
        // 保存的场馆id
        Integer hid = Integer.parseInt(request.getHeader("userId"));;

        // 根据场馆id以及教练id查询签约信息
        YogaGymSignedCoach ygsc = yogaGymSignedCoachService.selectByCoachIdAndGymId(hid, coachId);

        System.out.println(ygsc);
        if(coachId == null){
            return Result.Error(600,"参数格式错误");
        }
        if(ygsc != null && ygsc.getStatus() == 1){
            return Result.Error(600,"您已发送过签约请求!");
        }

        if( ygsc != null && ygsc.getStatus() == 2 ){
            return Result.Error(600,"您已签约该教练!");
        }

        Boolean flag = null;
        if(ygsc == null){
            flag  = yogaGymSignedCoachService.insetByGymIdAndCoachId(hid,coachId,1);
        }else {
            flag = yogaGymSignedCoachService.updataStatus(coachId, 1);
        }

        System.out.println(flag.booleanValue() == true);
        if(flag.booleanValue() == true){
            Result success = Result.Success();
            success.setMessage("签约请求发送成功");
            return  success;
        }


        return  Result.Error(600,"服务器繁忙!");
    }

    /**
     * @description:TODO(根据教练id查询已签约场馆)
     * @param coachId
     * @return:  org.example.Result
     * @author: Fy浪迹、
     * @date: 2020/7/18 21:21
     */
    @GetMapping("selectSigningCoachByCoachId")
    public Result selectSigningCoachByCoachId(Integer coachId){

        YogaGymSignedCoach ygsc = yogaGymSignedCoachService.query().eq("coach_id", coachId).eq("status", 2).one();
        return Result.Success(ygsc);
    }

    /**
     * 选择所有教练准备查看签约
     * @return
     */
    @GetMapping("selectAllCoach")
    public Result selectAllCoach(){

        List<Map<String, Object>> maps = coachInfoService.selectAllCoach();
        return Result.Success(maps);
    }

    /**
     * 发出邀请
     * @param request
     * @param CoachId
     * @param CoachInfoId
     * @return
     */
    @GetMapping("chooseCoach")
    public Result chooseCoach(HttpServletRequest request,int CoachId,int CoachInfoId){
        CoachInfo coachInfo = new CoachInfo();
        coachInfo.setB("2");
        coachInfo.setCoachInfoId(CoachInfoId);
        coachInfoService.saveOrUpdate(coachInfo);

        QueryWrapper<YogaGymSignedCoach> wrapper = new QueryWrapper<>();
        wrapper.eq("yoga_gym_id",Integer.parseInt(request.getHeader("userId")));
        wrapper.eq("coach_id",CoachId);
        YogaGymSignedCoach one = yogaGymSignedCoachService.getOne(wrapper);
        if(one!=null){
            return Result.Error(600,"您已经发出申请");
        }
        //加入瑜伽馆旗下的教练
        YogaGymSignedCoach coach = new YogaGymSignedCoach();
        coach.setStatus(1);
        coach.setCoachId(CoachId);
        coach.setYogaGymId(Integer.parseInt(request.getHeader("userId")));
        yogaGymSignedCoachService.save(coach);
        return Result.Success();
    }

    /**
     * 给教练加课
     * @param courseId
     * @param coachId
     * @return
     */
    @RequestMapping(value = "addCourse",method = RequestMethod.GET)
    public Result addCourse(int courseId,int coachId){
        CoachCourse course = new CoachCourse();
        course.setCoachId(coachId);
        course.setCourseId(courseId);

        coachCourseService.save(course);
        return Result.Success();
    }

    /**
     * 加课表
     * @param course
     * @return
     */
    @RequestMapping(value = "setAllCourse",method = RequestMethod.GET)
    public Result setAllCourse(Course course){

        courseService.save(course);
        return Result.Success();
    }

    /**
     * 节约教练
     * @param request
     * @param coachId
     * @return
     */
    @RequestMapping(value = "delCocah",method = RequestMethod.GET)
    public Result delCocah(HttpServletRequest request,int coachId){
        QueryWrapper<CoachInfo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("user_id",coachId);

        CoachInfo one = coachInfoService.getOne(wrapper1);
        one.setB("1");
        coachInfoService.updateById(one);


        QueryWrapper<YogaGymSignedCoach> wrapper = new QueryWrapper<>();
        wrapper.eq("coach_id",coachId);
        wrapper.eq("yoga_gym_id",Integer.parseInt(request.getHeader("userId")));
        yogaGymSignedCoachService.remove(wrapper);
        return Result.Success();

    }







}

